/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2022-2022. All rights reserved.
 */

import { FileSystemSnapshotHierarchyVisitor, SnapshotVisitResult } from '../generator/filesystem-snapshot-visitor.js';
import { FileType } from '../util/file-type.js';

import { Snapshot } from './snapshot.js';

/**
 * 快照抽象基类
 *
 * @since 2022/9/1
 */
export abstract class BasicFileSnapshot extends Snapshot {
  hashValue = '';
  name: string;
  path: string;
  type: FileType;
  isSymbolicLink: boolean;

  constructor(name = '', path = '', type = FileType.UNKNOWN, isSymbolicLink = false) {
    super();
    this.name = name;
    this.path = path;
    this.type = type;
    this.isSymbolicLink = isSymbolicLink;
  }

  /**
   * accept
   *
   * @param visitor
   */
  abstract accept(visitor: FileSystemSnapshotHierarchyVisitor): SnapshotVisitResult;

  /**
   * Generator 生成器 ，Depth First order
   */
  abstract walk(): IterableIterator<BasicFileSnapshot>;

  /**
   * equals
   *
   * @param other
   */
  abstract equals(other: BasicFileSnapshot): boolean;

  /**
   * isHashEquals
   *
   * @param other
   */
  isHashEquals(other: BasicFileSnapshot) {
    return this.hashValue === other.hashValue;
  }

  /**
   * toJsonString
   */
  toJsonString() {
    return JSON.stringify(this);
  }

  [Symbol.iterator]() {
    return this.walk();
  }
}
